//app.js
var config = require("/data/config.js")

function login(res) {
  wx.login({
    success: function (res) {
      var code = res.code;
      if (code) {
        console.log('获取用户登录凭证：' + code);
        // console.log(config.service.homeUrl);
        wx.request({
          url: config.service.homeUrl + '/huiguniang/wx/login?code=' + code,
          method: "POST",
          header: { "Content-Type": "application/x-www-form-urlencoded" },
          success: function (res) {
            console.log(res)
            let openid = res.data.result.openid;
            // console.log(openid)
            wx.setStorage({
              key: "openid",
              data: res.data.result
            })
            /* 注册 */
            wx.request({
              // url: config.service.homeUrl + '/huiguniang/registerByWX?openId=' + openid,
              url: config.service.homeUrl + '/huiguniang/user/select/Id?openId=' +openid,
              method: "POST",
              header: { "Content-Type": "application/x-www-form-urlencoded" },
              success: function (res) {
                let userInfo = {};
                let userId = {};
                // console.log(res)
                // userInfo.id = 62;//测试用的用户id
                userInfo = res.data.result;
                userId = res.data.result.openId;
                // console.log(userInfo.openId)
                wx.setStorage({
                  key: 'userId',
                  data: userId,
                })
                wx.setStorage({
                  key: "userInfo",
                  data: userInfo
                // if (res.data.status.code === 1) {
                //   /* 注册成功 */
                //   if (res.data.result === 1) {
                //     /* 通过openId 获取 userId */
                //     wx.request({
                //       url: config.service.homeUrl + '/user/select/userId?openId=' + openid,
                //       method: "GET",
                //       header: { 'Content-Type': 'application/json' },
                //       success: function (res) {
                //         let userInfo = {};
                //         userInfo.id = res.data.result;
                //         wx.setStorage({
                //           key: "userInfo",
                //           data: userInfo
                //         })
                //       }
                //     })
                //     /* 已注册 */
                //   } else if (res.data.result === 0) {
                //     /* 通过openId 获取 userId */
                //     wx.request({
                //       url: config.service.homeUrl + '/user/select/userId?openId=' + openid,
                //       method: "GET",
                //       header: { 'Content-Type': 'application/json' },
                //       success: function (res) {
                //         let userInfo = {};
                //         userInfo.id = res.data.result;
                //         wx.setStorage({
                //           key: "userInfo",
                //           data: userInfo
                //         })
                //       }
                //     })
                //   }
                // }
                })
              }
            })
            wx.redirectTo({
              url: '../home/home'
            })
          }
        })
      } else {
        // console.log('获取用户登录态失败：' + res.errMsg);
      }
    }
  });
}

App({

  data:{
    openId: ''
  },

  onLaunch: function () {
    // 展示本地存储能力
    wx.setStorageSync("config", config);
    var url = wx.getStorageSync("config");
    let homeUrl = config.service.homeUrl;
    let openId = wx.getStorageSync('openid');

    if (openId === '') {
      wx:wx.showModal({
        title: '提示',
        content: '您还未授权登陆，是否授权？',
        showCancel: true,
        cancelText: '否',
        cancelColor: '',
        confirmText: '是',
        confirmColor: '',
        success: function(res) {
          // console.log('showModal success');
          login(res);
        },
        fail: function(res) {
          // console.log('showModal fail')
        },
        complete: function(res) {},
      })
    }
  },

  globalData: {
    userInfo: null,
    userId: null
  }

})

